package com.app.service.impl;

import com.app.common.CalculateDistance;
import com.app.mapper.AreaMapper;
import com.app.mapper.CityMapper;
import com.app.mapper.EntrepreneursMapper;
import com.app.mapper.ProvinceMapper;
import com.app.model.*;
import com.app.service.EntrepreneursService;
import com.app.vo.EntrepreneursVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 创业
 *
 * @author 16693103420
 * @since 2024-02-01
 */
@Service
public class EntrepreneursServiceImpl extends ServiceImpl<EntrepreneursMapper, EntrepreneursModel> implements EntrepreneursService {
    @Autowired
    private EntrepreneursMapper entrepreneursMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    public ProvinceMapper provinceMapper;

    //创业列表
    @Override
    public EntrepreneursVo getEntrepreneursList(Integer page, Integer limit, AdminUserModel user) {
        Page<EntrepreneursModel> page1 = new Page<>(page, limit);
        QueryWrapper<EntrepreneursModel> qw = new QueryWrapper<>();
//        if (user.getRoleId() == 3) {
//            qw.eq("city_id", user.getAreaId());
//        }
//        if (user.getRoleId() == 4) {
//            qw.eq("area_id", user.getAreaId());
//        }
        qw.orderByDesc("create_time");
        Page<EntrepreneursModel> entrepreneursPage = entrepreneursMapper.selectPage(page1, qw);
        List<EntrepreneursModel> entrepreneursList = entrepreneursPage.getRecords();
//        for (EntrepreneursModel entrepreneursModel : entrepreneursList) {
//            //构建areaText
//            String areaText;
//            String businessAreaId = entrepreneursModel.getAreaId();
//            QueryWrapper<AreaModel> areaQW = new QueryWrapper<>();
//            areaQW.eq("area_id", businessAreaId);
//            AreaModel areaModel = areaMapper.selectOne(areaQW);
//            areaText = areaModel.getArea();
//            if (entrepreneursModel.getCityId() != null) {
//                CityModel cityModel = cityMapper.selectOne(new QueryWrapper<CityModel>()
//                        .eq("city_id", entrepreneursModel.getCityId()));
//                String city = cityModel.getCity();
//                ProvinceModel provinceModel = provinceMapper.selectOne(new QueryWrapper<ProvinceModel>()
//                        .eq("province_id", cityModel.getProvinceId()));
//                areaText = provinceModel.getProvince() +"\\" +city+"\\"+areaText;
//            }
//            entrepreneursModel.setAreaText(areaText);
//        }
        long count = entrepreneursPage.getTotal();
        EntrepreneursVo entrepreneursVo = new EntrepreneursVo();
        entrepreneursVo.setList(entrepreneursList);
        entrepreneursVo.setCount((int) count);
        return entrepreneursVo;
    }

    //创业列表
    @Override
    public EntrepreneursVo getUserEntrepreneursList(Integer page, Integer limit) {
        Page<EntrepreneursModel> page1 = new Page<>(page, limit);

        QueryWrapper<EntrepreneursModel> qw = new QueryWrapper<>();
        Page<EntrepreneursModel> entrepreneursPage = entrepreneursMapper.selectPage(page1, qw);
        List<EntrepreneursModel> entrepreneursList = entrepreneursPage.getRecords();
//        for (EntrepreneursModel entrepreneursModel : entrepreneursList) {
//            //计算用户与商家距离
//            Double businessLongitude = entrepreneursModel.getLongitude();
//            Double businessLatitude = entrepreneursModel.getLatitude();
////            Double number = CalculateDistance.haversine(businessLatitude, businessLongitude, latitude, longitude);
////            Double distance = Double.valueOf(String.format("%.1f", number));
////            entrepreneursModel.setDistance(distance);
//        }
        long count = entrepreneursPage.getTotal();
        EntrepreneursVo entrepreneursVo = new EntrepreneursVo();
        entrepreneursVo.setList(entrepreneursList);
        entrepreneursVo.setCount((int) count);
        return entrepreneursVo;
    }

    @Override
    public EntrepreneursModel getUserById(Integer id, Double longitude, Double latitude) {
        EntrepreneursModel entrepreneursModel = entrepreneursMapper.selectById(id);
        //计算用户与商家距离
//        Double businessLongitude = entrepreneursModel.getLongitude();
//        Double businessLatitude = entrepreneursModel.getLatitude();
//        Double number = CalculateDistance.haversine(businessLatitude, businessLongitude, latitude, longitude);
//        Double distance = Double.valueOf(String.format("%.1f", number));
//        entrepreneursModel.setDistance(distance);
        return entrepreneursModel;
    }
}
